<template>
	<view class="content">
		<view class="navbar" :style="{position:headerPosition,top:headerTop}">
			<view class="nav-item" :class="{current: filterIndex === index}" @click="tabClick(index,item)" v-for="(item,index) in flist" :key="item.id" v-if="index<5">
				{{item.cName}}
			</view>
	<!-- 		<view class="nav-item" :class="{current: filterIndex === 1}" @click="tabClick(1)">
				生鲜水果                 
			</view>
			
			<view class="nav-item" :class="{current: filterIndex === 2}" @click="tabClick(2)">
				 海鲜水产
			</view>
			<view class="nav-item" :class="{current: filterIndex === 3}" @click="tabClick(3)">
				 异国风味
			</view>
			<view class="nav-item" :class="{current: filterIndex === 4}" @click="tabClick(4)">
				 优选饮食
			</view> -->
		</view>
		<view class="cate-section" v-for="item in currenttab.lsChildClass" :key="item.id">
			<view class="posi">{{item.cName}}</view>
			<view class="item" v-for="titem in item.lsChildClass" :key="titem.id" @click="navToList(titem)">
				<view class="item-img"><image :src="titem.fullImageUrl" mode=""></image></view>
				<text class="item-text">{{titem.cName}}</text>
			</view>
			
			<!-- <view class="item all"  >
				<view class="item-img"><image class="add" src="../../static/img/5/5-3.png" mode=""></image></view>
				<text class="item-text">全部分类</text>
			</view> -->
		</view>
		<view class="flex">
			<text>热品推荐</text>
			<!-- <text class="color_9">更多推荐</text> -->
		</view>
		<view class="goods-list">
			<view 
				v-for="(item, index) in goodsList" :key="index"
				class="goods-item"
				@click="navToDetailPage(item)"
			>
				<view class="image-wrapper">
					<image :src="item.defaultAlbum" mode="aspectFill"></image>
				</view>
				<text class="title clamp">{{item.pName}}</text>
				<view class="recomd">
					<!-- <text class="recomd-btn">{{item.iSales}}</text> -->
				</view>
				<view class="price-box">
					¥{{item.fSalePrice}}
				</view>
				
			</view>
			
			
		</view>
		
		<!-- 
		<view class="cate-mask" :class="cateMaskState===0 ? 'none' : cateMaskState===1 ? 'show' : ''" @click="toggleCateMask">
			<view class="cate-content" @click.stop.prevent="stopPrevent" @touchmove.stop.prevent="stopPrevent">
				<scroll-view scroll-y class="cate-list">
					<view v-for="item in cateList" :key="item.id">
						<view class="cate-item b-b two">{{item.name}}</view>
						<view 
							v-for="tItem in item.child" :key="tItem.id" 
							class="cate-item b-b" 
							:class="{active: tItem.id==cateId}"
							@click="changeCate(tItem)">
							{{tItem.name}}
						</view>
					</view>
				</scroll-view>
			</view>
		</view> -->
		<!-- 专题列表 -->
		<view class="special-section">
			<view class="biaoti">
				相关专题
			</view>
			<view class="special-item bg_ff"  v-for="(item, index) in specialList" :key="index">
				<navigator :url= "'pages/special/detial?id='+item.id">
					<view class="text-tittle">
						<image class="icon-huo" v-if="item.is_hot == 1" src="../../static/img/num_d/huo.png" mode="aspectFill"></image>
						<text>{{item.name}}</text>
						
					</view> 
					<image class="item-img"  :src="item.cover_image" mode="aspectFill"></image>
					<view class="special-text">
						
						<view class="color_9 text-small">
							<image class="eye" src="../../static/img/7/7-11.png"  mode="aspectFill"></image> {{item.look}}人看过
							<image class="heart" src="../../static/img/7/7-10.png" mode="aspectFill"></image> {{item.collect}}
						</view>
					</view>
				</navigator>
			</view>
		</view>
		<!-- <uni-load-more :status="loadingType"></uni-load-more> -->
	</view>
</template>

<script>
	// import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import ListOther from '@/components/list/listOther.vue';
	export default {
		components: {
			
				ListOther,
		},
		data() {
			return {
				cateMaskState: 0, //分类面板展开状态
				headerPosition:"fixed",
				headerTop:"0px",
				loadingType: 'more', //加载更多状态
				filterIndex: 0, 
				cateId: 0, //已选三级分类id
				priceOrder: 0, //1 价格从低到高 2价格从高到低
				cateListop: [
					{
						image: "../../static/img/1/17.png",
						text: "牛油果",
						pid: 1,
					},{
						image: "../../static/img/1/17.png",
						text: "香蕉",
						pid: 2,
					},{
						image: "../../static/img/1/17.png",
						text: "苹果",
						pid: 3,
					},{
						image: "../../static/img/1/17.png",
						text: "葡萄",
						pid: 4,
					},{
						image: "../../static/img/1/17.png",
						text: "黄桃",
						pid: 5,
					},{
						image: "../../static/img/1/17.png",
						text: "杏子",
						pid: 6,
					},{
						image: "../../static/img/1/17.png",
						text: "杏子",
						pid: 6,
					}
				],
				cateList:[],
				goodsList: [],
				List: [
					{
						image: "../../static/img/1/17.png",
						text: "三文鱼美味攻略",
						id: 1,
						price:'19.9',
						liulan:"1.5万",
						zan:"1000"
					},{
						image: "../../static/img/1/17.png",
						text: "三文鱼美味攻略",
						id: 2,
						price:'19.9',
						liulan:"1.5万",
						zan:"1000"
					},{
						image: "../../static/img/1/17.png",
						text: "三文鱼美味攻略",
						id: 3,
						price:'19.9',
						liulan:"1.5万",
						zan:"1000"
					},
				],
				currentId: 1,
				flist: [],
				slist: [],
				tlist: [],
				currenttab:[],
				specialList:[],
				current:1
			};
		},
		
		onLoad(options){
			// #ifdef H5
			this.headerTop = document.getElementsByTagName('uni-page-head')[0].offsetHeight+'px';
			// #endif
			// this.cateId = options.tid;
		
			let that = this;
			let token=that.$SysCache.get('token');
			that.$Request.get(
				that.$api.httpApi.cate.cate+"&token="+that.$SysCache.get('token')
				
				).then(res=>{
					if(res.code == 200){
						console.log(res)
						let list =res.data;
						that.flist=res.data
						console.log(list)
						list.forEach((item,index)=>{
							if(index == 0){
								that.currentId = item.id;
								that.currenttab = item
							}
							
						}) 
						that.loadCateList(options.fid,options.sid);
						that.loadData();
					}
				})
				
		},
		onPageScroll(e){
			//兼容iOS端下拉时顶部漂移
			if(e.scrollTop>=0){
				this.headerPosition = "fixed";
			}else{
				this.headerPosition = "absolute";
			}
		},
		//下拉刷新
		onPullDownRefresh(){
			// this.loadData('refresh');
		},
		//加载更多
		onReachBottom(){
			// this.loadData();
		},
		methods: {
			//加载分类
			async loadCateList(fid, sid){
				let list = await this.$api.json('cateList');
				let cateList = list.filter(item=>item.pid == fid);
				
				cateList.forEach(item=>{
					let tempList = list.filter(val=>val.pid == item.id);
					item.child = tempList;
				})
				this.cateList = cateList;
				this.currentId = cateList[0].id
			},
			//加载商品 ，带下拉刷新和上滑加载
			async loadData(type='add', loading) {
				//没有更多直接返回
				if(type === 'add'){
					if(this.loadingType === 'nomore'){
						return;
					}
					this.loadingType = 'loading';
				}else{
					this.loadingType = 'more'
				}
				
				// let goodsList = await this.$api.json('goodsList');
				let orders = {
						fieldname: "iOrder",
						direction: "asc"
					}
				this.$Request.post(
					this.$api.httpApi.search.search + "&token=" + this.$SysCache.get('token'), {
						queryCondition: {
							order: orders,
							pageindex: this.current,
							pagesize: 12,
							where: [{
								fieldname: "categoryid",
								valuefrom: this.currentId
							}]
						}
					}
				).then(res => {
					if (res.code == 200) {
						this.goodsList = res.data.data
					}
				})
				
				
				// if(type === 'refresh'){
				// 	this.goodsList = [];
				// }
				//筛选，测试数据直接前端筛选了
				// if(this.filterIndex === 1){
				// 	goodsList.sort((a,b)=>b.sales - a.sales)
				// }
				// if(this.filterIndex === 2){
				// 	goodsList.sort((a,b)=>{
				// 		if(this.priceOrder == 1){
				// 			return a.price - b.price;
				// 		}
				// 		return b.price - a.price;
				// 	})
				// }
				
				
				
				//判断是否还有下一页，有是more  没有是nomore(测试数据判断大于20就没有了)
				// this.loadingType  = this.goodsList.length > 20 ? 'nomore' : 'more';
				// if(type === 'refresh'){
				// 	if(loading == 1){
				// 		uni.hideLoading()
				// 	}else{
				// 		uni.stopPullDownRefresh();
				// 	}
				// }
				
				
				this.$Request.post4(
					this.$api.httpApi.special.categoryTopic, {
						class_id: this.currentId
					}
				).then(res => {
					if (res.code == 1) {
						this.specialList = res.data
						
				
					}
				})
				
			},
			//筛选点击
			tabClick(index,item){
				this.filterIndex = index;
				this.currentId = item.id;
				this.currenttab = item
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				})
				this.loadData('refresh', 1);
				uni.showLoading({
					title: '正在加载'
				})
			},
			//显示分类面板
			toggleCateMask(type){
				let timer = type === 'show' ? 10 : 300;
				let	state = type === 'show' ? 1 : 0;
				this.cateMaskState = 2;
				setTimeout(()=>{
					this.cateMaskState = state;
				}, timer)
			},
			//分类点击
			changeCate(item){
				this.cateId = item.id;
				this.toggleCateMask();
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				})
				this.loadData('refresh', 1);
				uni.showLoading({
					title: '正在加载'
				})
				
			},
			navToList(item){
				let id = item.id
				let name = item.cName
				uni.navigateTo({
					url: `/pages/cate/cateList?categoryid=${id}&name=${name}`
				})
			},
			//详情
			navToDetailPage(item){
				//测试数据没有写id，用title代替
				let id = item.pid;
				uni.navigateTo({
					url: `/pages/detail/index?id=${id}`
				})
			},
			
		},
		onNavigationBarButtonTap(e) {
			console.log(e)
		  uni.navigateTo({
		  	url: `/pages/cate/index`
		  })
		}
	}
</script>

<style lang="scss">
	page, .content{
		background: #F2F2F2;
	}
	.content{
		padding-top: 96upx;
	}

	.navbar{
		position: fixed;
		left: 0;
		top: var(--window-top);
		display: flex;
		width: 100%;
		height: 80upx;
		background: #000;
		box-shadow: 0 2upx 10upx rgba(0,0,0,.06);
		z-index: 10;
		.nav-item{
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 25upx;
			color: #fff;
			position: relative;
			&.current{
				color: #fff;
				&:after{
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 120upx;
					height: 0;
					border-bottom: 4upx solid #4DC897;
				}
			}
		}
		.p-box{
			display: flex;
			flex-direction: column;
			.yticon{
				display: flex;
				align-items: center;
				justify-content: center;
				width: 30upx;
				height: 14upx;
				line-height: 1;
				margin-left: 4upx;
				font-size: 26upx;
				color: #fff;
				&.active{
					color: #4DC897;
				}
			}
			.xia{
				transform: scaleY(-1);
			}
		}
		.cate-item{
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			width: 80upx;
			position: relative;
			font-size: 44upx;
			&:after{
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				border-left: 1px solid #ddd;
				width: 0;
				height: 36upx;
			}
		}
	}

	/* 分类 */
	.cate-mask{
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0,0,0,0);
		z-index: 95;
		transition: .3s;
		
		.cate-content{
			width: 630upx;
			height: 100%;
			background: #fff;
			float:right;
			transform: translateX(100%);
			transition: .3s;
		}
		&.none{
			display: none;
		}
		&.show{
			background: rgba(0,0,0,.4);
			
			.cate-content{
				transform: translateX(0);
			}
		}
	}
	.cate-list{
		display: flex;
		flex-direction: column;
		height: 100%;
		.cate-item{
			display: flex;
			align-items: center;
			height: 90upx;
			padding-left: 30upx;
 			font-size: 28upx;
			color: #555;
			position: relative;
		}
		.two{
			height: 64upx;
			color: #303133;
			font-size: 30upx;
			background: #f8f8f8;
		}
		.active{
			color: $base-color;
		}
	}

	/* 商品列表 */
	.goods-list{
		display:flex;
		flex-wrap:wrap;
		padding: 0 30upx;
		background: #F2F2F2;
		.goods-item{
			display:flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40upx;
			margin-top: 25upx;
			background: #fff;
			border-radius:15upx;
			&:nth-child(2n+1){
				margin-right: 4%;
			}
		}
		.image-wrapper{
			width: 100%;
			height: 330upx;
			border-radius: 3px;
			overflow: hidden;
			background: #fff;
			image{
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}
		.title{
			font-size: 28upx;
			color: #444444;
			line-height: 55upx;
			padding-top: 10upx;
			text-align: center;
		}
		.price-box{
			font-size: 24upx;
			color: #F0406F;
			text-align: center;
		}
		.price{
			padding: 0 12upx;
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;
			text-align: center;
			&:before{
				content: '￥';
				font-size: 26upx;
			}
		}
		.recomd-btn{
			display: block;
			text-align: center;
			font-size: 21upx;
			color: #888888;
		}
	}
	
	.cate-section{
		display:flex;
		flex-wrap:wrap;
		padding: 0 30upx;
		background: #fff;
		justify-content: flex-start;
		position: relative;
		.posi{
			position: absolute;
			top: 10upx;
			left: 50upx;
			font-size: 28upx;
			font-weight: bold;
		}
		.item{
			display:flex;
			flex-direction: column;
			width: 25%;
			// margin: 0 auto;
			padding-bottom: 20upx;
			background: #fff;
			border-radius:15upx;
			.item-img{
				width:125upx;
				height:125upx;
				margin: auto;
				border:1px solid rgba(238, 238, 238, 1);
				border-radius:10upx;
				padding: 24upx 8upx;
				image{
					width:109upx;
				    height:88upx;
				}
				image.add{
					width:43upx;
					height:42upx;
					margin:8px 14px 0 17px;;
				}
			}
		}
	}
	
	
	
	.item-text{
		color: #666666;
		font-size:24upx;
		text-align: center;
	}
.special-section{
		padding-bottom: 50upx;
	}
	.special-item{
		width: 95%;
		margin: 0 auto;
		margin-top: 20upx;
		background: #fff;
		padding:0 30upx;
	}
	.cate-section {
		display: flex;
		// justify-content: space-around;
		align-items: center;
		flex-wrap:wrap;
		padding: 60upx 22upx 0upx 22upx;  
		background: #fff;
		.cate-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size:24upx;
			font-family:Microsoft YaHei;
			font-weight:400;
			color:#333;
		}
		/* 原图标颜色太深,不想改图了,所以加了透明度 */
		image {
			width:111upx;
			height: 111upx;
			margin-bottom: 14upx;
			border-radius: 50%;
		}
	}
	.special-text{
	   padding: 20upx 0upx;
	
	}
	.text-tittle{
		padding: 20upx 0 ;
	}
	.text-tittle  text{
		color: #444444;
		font-size: 26upx;
		
	}
	.text-tittle image{
		width: 39upx;
		height: 39upx;
		vertical-align: middle;
		margin-right: 10upx;
	}
	.text-small image{
		vertical-align: middle;
		margin-left:  30upx;
		margin-right: 8upx;
	}
	.text-small .eye{
		width: 30upx;
		height: 17upx;
	}
	.text-small .heart{
		width: 25upx;
		height: 23upx;
	}
	.special-text .text-tittle{
		font-size: 27upx;
		margin: 15upx 0;
	}
	.text-small{
		font-size: 18upx;
	}
	.red{
		color: #F0406F;
		float: right;
	}
	.item-img{
		width: 100%;
		height: 450upx;
	}
	.flex{
		display: flex;
		justify-content: space-between;
		font-size: 25upx;
		padding: 40upx 30upx;
	}
	.biaoti{
		font-size: 25upx;
		padding: 40upx 30upx;
		text-align: center;
	}
</style>


